var SGModel           = require('./SGModel');
var SGProto           = require('./SGProto');
var RoomProto         = require('../../API/RoomProto');
var HallApi           = require('../../API/HallAPI');
var RoomAPI           = require('../../API/RoomAPI');
var RoomMessageRouter = 'game.gameHandler.roomMessageNotify';
var GameMessageRouter = 'game.gameHandler.gameMessageNotify';

cc.Class({
	extends: cc.Component,
	properties: {
	},

	onLoad: function () {
		this.emotionLayout = cc.find('Panel/EmotionScrollView/view/content', this.node);
		let emotionNode = cc.find('emotion', this.node);
		for (let i = 0; i <= 23; ++i) {
			let node = cc.instantiate(emotionNode);
			Global.CCHelper.updateSpriteFrame(`Chat/Emotion/${i}/${i}_se`, node.getComponent(cc.Sprite)); 
			node.on(cc.Node.EventType.TOUCH_END, this.onEmotionClick.bind(this, i), this);
			node.parent = this.emotionLayout;
			node.active = true;
		}
	},

	onEmotionClick: function (param) {
        Global.AudioManager.playCommonSoundClickButton();
		if (this.getCanSendChat()) {
			Global.NetworkManager.notify(GameMessageRouter, SGProto.gameChatNotify(1, parseInt(param), null));
		}
		else {
			Global.DialogManager.addTipDialog(`聊天太频繁，请${Global.Constant.chatCool}秒后再发！`);
		}
		this.onCloseClick();
	},

	onTextClick: function (event, param) {
        Global.AudioManager.playCommonSoundClickButton();
		let text = [ '',
			'快点啊，等到花儿都谢了！',
			'不要吵了，不要吵了。专心玩游戏吧！',
			'怎么又断线了，网络怎么这么差啊！',
			'不要走，决战到天亮！',
			'底牌亮出来绝对吓死你！',
			'风水轮流转，底裤都输光！',
			'看我通杀全场，这些钱都是我的！',
			'不好意思啊，我要离开一会！',
		][parseInt(param)];
		if (this.getCanSendChat()) {
			Global.NetworkManager.notify(GameMessageRouter, SGProto.gameChatNotify(2, text, null));
		}
		else {
			Global.DialogManager.addTipDialog(`聊天太频繁，请${Global.Constant.chatCool}秒后再发！`);
		}
		this.onCloseClick();
	},

	onButtonClick: function (event, param) {
        Global.AudioManager.playCommonSoundClickButton();
		cc.find('bg', this.node).active = true;
		cc.find('Panel', this.node).active = true;
		cc.find('Button', this.node).active = false;
	},

	onChatClick: function (event, param) {
        Global.AudioManager.playCommonSoundClickButton();
		let edit = cc.find('Panel/EditBox', this.node);
		let text = edit.getComponent(cc.EditBox).string;
		if (text.length > 0) {
			if (this.getCanSendChat()) {
				Global.NetworkManager.notify(GameMessageRouter, SGProto.gameChatNotify(2, text, null));
			}
			else {
				Global.DialogManager.addTipDialog(`聊天太频繁，请${Global.Constant.chatCool}秒后再发！`);
			}
			this.onCloseClick();
		}
	},

	onCloseClick: function (event, param) {
		if (event) {
			Global.AudioManager.playCommonSoundClickButton();
		}
		cc.find('bg', this.node).active = false;
		cc.find('Panel', this.node).active = false;
		cc.find('Button', this.node).active = true;
		let edit = cc.find('Panel/EditBox', this.node);
		edit.getComponent(cc.EditBox).string = '';
	},

	onToggleClick: function (event, param) {
		if (param == 'emotion') {
			cc.find('Panel/EmotionScrollView', this.node).active = true;
			cc.find('Panel/MsgScrollView', this.node).active = false;
		}
		else if (param == 'msg') {
			cc.find('Panel/EmotionScrollView', this.node).active = false;
			cc.find('Panel/MsgScrollView', this.node).active = true;
		}
	},

	getCanSendChat: function () {
		if (this.chatRecord == null) {
			this.chatRecord = Date.now();
			console.log(Date.now(), this.chatRecord, "1");
			return true;
		}
		else if (Date.now()-this.chatRecord >= Global.Constant.chatCool*1000) {
			console.log(Date.now(), this.chatRecord, "2");
			this.chatRecord = Date.now();
			return true;
		}
		return false;
	},
});

